//------------------ API Helper Classes ------------------//
Gamedu = function() {
};
Player = function() {
};
Game = function() {
};
GameStats = function() {
};
GameBlockly = function() {
};

Player.prototype = {
	id : "1",
	name : "Aluno-Teste"
};

Game.prototype = {
	player : new Player(),
	maxTries : 3,
	maxTime : 60,
	gameStats : new GameStats()
};

GameStats.prototype = {
	victory : 0,
	tries : 0,
	time : 0
};

GameBlockly.prototype = {
	eid : 0,
	speedSlider: 0,
	path: [],
	hasBlock: 0,
	moveUp:function(id,uid){
		this.path.push(["up", id, uid]);
		this.hasBlock += 1;
	},
	moveLeft:function(id,uid){
		this.path.push(["left",id, uid]);
		this.hasBlock += 1;
	},
	
	moveDown:function(id,uid){
		this.path.push(["down",id, uid]);
		this.hasBlock += 1;
	},
	
	moveRight:function(id,uid){
		this.path.push(["right",id, uid]);
		this.hasBlock += 1;
	},
	drop:function(id,uid){
		this.path.push(["drop", id, uid]);
		this.hasBlock += 1;
	},
	hold:function(id,uid){
		this.path.push(["hold", id, uid]);
		this.hasBlock += 1;
	},
	shoot:function(id,uid){
		this.path.push(["shoot", id, uid]);
		this.hasBlock += 1;
	},
	reset: function(){
		this.eid = 0;
		this.path = [];
		this.hasBlock = 0;
		this.restart();
	},
	restart: function (){
		this.eid = 0;
		this.path = [];
		this.hasBlock = 0;
		this.path.push(["restart", 0, 0]);
		this.hasBlock = 1;
	},
	execute: function(){
		Blockly.mainWorkspace.traceOn(true);
		var code = Blockly.Generator.workspaceToCode('JavaScript');
		try {
			//Executa Comandos do Blockly, colocando-os em path
			eval(code);
		} catch (e) {
			if (e !== null) {
				alert(e);
			}
		}	
	},
	executeNextBlock: function(){
		var tuple = this.path.shift();
		console.log(this.hasBlock);
		console.log(tuple);
		try{
			Blockly.mainWorkspace.highlightBlock(tuple[1]);
			this.hasBlock -= 1;
			return [tuple[0],tuple[2]];
		}catch (e) {
			return "";
		}	
	},
	showCode: function(){
		var code = Blockly.Generator.workspaceToCode('JavaScript');
		// Strip out serial numbers.
		code = code.replace(/(,\s*)?'\d+'\)/g, ')');
		alert(code);
	}
};

//------------------ API Main Class ------------------//
Gamedu.prototype = {
	game : new Game(),
	blockly: new GameBlockly(),
	startGame : function() {
		return this.game;
	},
	getHasBlocks : function() {
		return gamedugame.blockly.hasBlock;
	},
	getExecuteNextBlock: function() {
		return this.blockly.executeNextBlock();
	},
	setGameStats : function(params) {//Declaração correta, porém, falta configurar alguma coisa no runtime. =/
		try {
			this.game.gameStats.victory = params[0];
			this.game.gameStats.tries = params[1];
			this.game.gameStats.time = params[2];
			console.debug(this.game);
		} catch(e) {
			console.debug("falhou");
		}
		return this.game;
	}
};


//------------------ API Objects ------------------//
var gamedugame = new Gamedu(); 